package com.zhiyizhen.handheld.component.execute.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.zhiyizhen.handheld.R;
import com.zhiyizhen.handheld.common.AppUtil;
import com.zhiyizhen.handheld.common.DateUtil;
import com.zhiyizhen.handheld.common.ImageLoader;
import com.zhiyizhen.handheld.common.LogUtil;
import com.zhiyizhen.handheld.common.ResourcesUtil;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseAdapter;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseViewHolder;
import com.zhiyizhen.handheld.component.execute.bean.ChargeRecordBean;
import com.zhiyizhen.handheld.component.execute.bean.ExecuteUserBean;
import com.zhiyizhen.handheld.component.main.bean.EnumFieldBean;
import com.zhiyizhen.handheld.component.main.bean.ExecTypeBean;
import com.zhiyizhen.handheld.databinding.ItemChargeRecordBinding;
import com.zhiyizhen.handheld.databinding.ItemExecuteBinding;

import java.util.List;

/**
 * 收费记录
 */
public class ChargeRecordAdapter extends BaseAdapter<ChargeRecordBean, ChargeRecordAdapter.ChargeRecordViewHolder> {
    private Context context;

    private int selectPosition = 0;
    private List<EnumFieldBean> payStatusEnum;
    public ChargeRecordAdapter(Context context) {
        super(context);
        this.context = context;
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
        notifyDataSetChanged();
    }

    public void setPayStatusEnum(List<EnumFieldBean> payStatusEnum) {
        this.payStatusEnum = payStatusEnum;
    }

    @NonNull
    @Override
    public ChargeRecordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ChargeRecordViewHolder(this, ItemChargeRecordBinding.inflate(LayoutInflater.from(parent.getContext())));
    }

    class ChargeRecordViewHolder extends BaseViewHolder<ChargeRecordBean> {

        private final ItemChargeRecordBinding binding;

        public ChargeRecordViewHolder(BaseAdapter<ChargeRecordBean, ChargeRecordViewHolder> adapter, ItemChargeRecordBinding viewBinding) {
            super(adapter, viewBinding.getRoot());
            binding = viewBinding;
        }

        @Override
        public void bindData(ChargeRecordBean data, int position) {
            LogUtil.d("position == " + position);
            LogUtil.d("ExecuteUserBean == " + data.toString());
            binding.dateTv.setText(data.getOrderTime());
            binding.moneyTv.setText(AppUtil.fenToYuan(data.getSubtotal()) + "元");
            binding.executePersonTv.setText(data.getOrderBy());
            binding.executeTimeTv.setText(data.getOrderTime());

            for (int i = 0; i < payStatusEnum.size(); i++) {
                if (TextUtils.equals(data.getPayStatus(), payStatusEnum.get(i).getValue())) {
                    binding.payStatusTv.setText(payStatusEnum.get(i).getText());
                }
            }

            binding.chargeInfoLayout.setVisibility(View.GONE);
            binding.moreImg.setImageResource(R.drawable.icon_up_arrow);
            binding.moreImg.setOnClickListener(v -> {
                if (binding.chargeInfoLayout.getVisibility() == View.VISIBLE) {
                    binding.chargeInfoLayout.setVisibility(View.GONE);
                    binding.moreImg.setImageResource(R.drawable.icon_up_arrow);
                } else {
                    binding.chargeInfoLayout.setVisibility(View.VISIBLE);
                    binding.moreImg.setImageResource(R.drawable.icon_down_arrow);
                }
            });
            binding.dateTv.setOnClickListener(v -> {
                if (binding.chargeInfoLayout.getVisibility() == View.VISIBLE) {
                    binding.chargeInfoLayout.setVisibility(View.GONE);
                    binding.moreImg.setImageResource(R.drawable.icon_up_arrow);
                } else {
                    binding.chargeInfoLayout.setVisibility(View.VISIBLE);
                    binding.moreImg.setImageResource(R.drawable.icon_down_arrow);
                }
            });
            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        }
    }
}
